﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Bling.DSL;
using Bling.WPF;
using Bling.Util;

namespace Bling.Tutorial {
  public class Chess : Canvas {
    public Chess() {
      CanvasBl MainCanvas = this;

      ThumbBl sun = new ThumbBl(MainCanvas) {
        LeftTop = new PointBl(10, 10), Background = Brushes.Yellow,
        Size = new PointBl(20, 20), CanDrag = true,
        Map = (self,p) => p.Clamp(new PointBl(0, 0), MainCanvas.Size - self.Size),
        ZIndex = 100,
      };

      PointBl PieceSize = Properties.Resources.piece.LoadBitmap().Size();

      int ncolumns = 5;
      int nrows = 5;

      PointBl slice = MainCanvas.Size / new PointBl(ncolumns, nrows);

      for (int i = 0; i < ncolumns; i++) {
        DoubleBl x = i * slice.X + slice.X / 2d;
        for (int j = 0; j < nrows; j++) {
          DoubleBl y = j * slice.Y + slice.Y / 2d;

          ImageBl piece = new ImageBl(MainCanvas) {
            Source = Properties.Resources.piece,
            ZIndex = 1, IsHitTestVisible = false,
          };
          ThumbBl thumb = new ThumbBl(MainCanvas) {
            Size = piece.Size,
            Background = Brushes.Transparent,
            Foreground = Brushes.Transparent,
            ZIndex = 0, Opacity = .01,
            Map = (self,p) => p.Clamp(new PointBl(0, 0), MainCanvas.Size - self.Size),
            CanDrag = true,
            CenterPosition = new PointBl(x,y),
          };
          piece.CenterPosition = thumb.CenterPosition;
          ImageBl highlight = new ImageBl(MainCanvas) {
            CenterTop = piece.CenterTop, ZIndex = 2,
            Source = Properties.Resources.highlight,
          };

          highlight.RenderTransform[piece.CenterPosition - highlight.LeftTop].Rotate = 
            (sun.CenterPosition - piece.CenterPosition).Angle.ToDegrees() + 90.ToDegrees();

          highlight.Opacity = .5 + (1d - (sun.CenterPosition - piece.CenterPosition).Length / MainCanvas.Size.Length) * .5;
        }
      }



    }


  }
}
